
/*
 * js for send a email to change password
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

$(function() {
  
   var email = $( "#email" ),
   allFields = $( [] ).add( email ),
   tips = $( ".validateTips" );
 
   function updateTips( t ) {
      tips
      .text( t )
      .addClass( "ui-state-highlight" );
      setTimeout(function() {
         tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
   }
  
   function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o) ) ) {
         tips.addClass( "ui-state-error" );
         updateTips( n );
         return false;
      } else {
         return true;
      }
   }
    
   function checkEmpty(email) {
      
      if ( email == "" ) {
         updateTips( "Email can't be empty");
         return false;
      }
       
      return true;
   }
    
 
   $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 250,
      width: 350,
      modal: true,
      buttons: {
         "Send": function() {
            var email = $( "#email").val();
            var bValid = true;
            allFields.removeClass( "ui-state-error" );
            bValid = bValid && checkEmpty(email);
            bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Invalid email eg. test@gmail.com" );
         
            if ( bValid ) {
              
               $.ajax({
                  type : "POST",
                  url : '../user/ajaxChangePassword',
                  cache : false,
                  data : "email=" + email,
                  dataType : "html",
                  success : function(data) {
                     console.log(data);
                     if(data == 'valid'){
                        $('.alert-success').show();
                        setTimeout(function(){
                           $(".alert-success").hide()
                        }, 5000);   
                     }else{
                        $('.alert-error').show();
                        setTimeout(function(){
                           $(".alert-error").hide()
                        }, 5000);   
                     }
                           
                  },
                  beforeSend: function() { 
                     $('.image-loader').addClass("loading"); 
                  },
                  complete: function() { 
                     $('.image-loader').removeClass("loading"); 
                  }  
                          
               });
          
               $( this ).dialog( "close" );
            }
         },
         Back: function() {
            tips.text("");
            $( this ).dialog( "close" );
         }
      },
      close: function() {
         tips.text("");
         allFields.val( "" ).removeClass( "ui-state-error" );
      }
   });
 
   $(".update_psw").click(function(event) {
      event.preventDefault();
      $( "#dialog-form" ).dialog( "open" );
   });

});
